package com.yanqu.road.server.cmd.cross.tradewar;

import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.bussiness.player.UserPatronsBussiness;
import com.yanqu.road.logic.pb.PatronsPb;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.pb.patrons.PatronsProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Map;

@Cmd(code = Protocol.S_CROSS_TRADE_WAR_CROSS_QUERY_ENEMY_PATRONS, desc = "跨服查询对手门客信息")
public class CrossTradeWarCrossQueryEnemyPatronsCmd extends AbstractCommand {

	private int activityId;
	private long userId;
	private long serverId;
	private long enemyUserId;
	private long enemyServerId;
	private Map<Integer, UserPatrons> patronsMap;
	private BigInteger enemyAbility = BigInteger.ZERO;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CrossTradeWarProto.CrossTradeWarCrossQueryEnemyPatronsReqMsg reqMsg = CrossTradeWarProto.CrossTradeWarCrossQueryEnemyPatronsReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		userId = reqMsg.getMyUserId();
		serverId = reqMsg.getMyServerId();
		enemyUserId = reqMsg.getEnemyUserId();
		enemyServerId = reqMsg.getEnemyServerId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		GamePlayer enemyPlayer = GamePlayerMgr.getOnlinePlayer(enemyUserId);
		if(null != enemyPlayer){
			patronsMap = enemyPlayer.getModule(PatronsModule.class).getUserPatronsMap();
		}else {
			patronsMap = UserPatronsBussiness.getUserPatronsMap(enemyUserId);
		}
		enemyAbility = PatronsMgr.getPatronsListAbility(new ArrayList<>(patronsMap.values()));
	}

	@Override
	public void sendData(GamePlayer player) {
		CrossTradeWarProto.CrossTradeWarCrossQueryEnemyPatronsRespMsg.Builder respMsg = CrossTradeWarProto.CrossTradeWarCrossQueryEnemyPatronsRespMsg.newBuilder();
		respMsg.setActivityId(activityId);
		respMsg.setRet(0);
		respMsg.setMyUserId(userId);
		respMsg.setMyServerId(serverId);
		respMsg.setEnemyPatronsAbility(enemyAbility.toString());
		for(UserPatrons userPatrons : patronsMap.values()){
			PatronsProto.PatronsTempMsg.Builder patronsMsg = PatronsPb.parsePatronsTempMsg(userPatrons);
			if(null != patronsMsg){
				respMsg.addEnemyPatronsList(patronsMsg);
			}
		}
		int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, userId);
		GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_TRADE_WAR_QUERY_ENEMY_PATRONS_RESP, respMsg, 0, activityId, groupId);
	}
}
